package com.cool.controller.shopcontroller;

import com.cool.bo.brandbo.BrandBo;
import com.cool.bo.shopbo.ShopBo;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.brand.BrandPo;
import com.cool.po.shoppo.ShopPo;
import com.cool.service.shop.ShopService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 门店 controller层
 * 黎秦赭
 */
@RestController
public class ShopController {
    @Autowired
    private ShopService shopService;

    private static final Integer PAGE_SIZE = 5;


    //1. 分页查询所有数据  测试数据: {"pageIndex":3}
    @PostMapping("/shop/findAll")
    public CommonResult findAll(@RequestBody ShopBo shopBo){
        PageHelper.startPage(shopBo.getPageIndex(),PAGE_SIZE);
        List<ShopPo> all = shopService.findAll();
        PageInfo<ShopPo> shopPoPageInfo = new PageInfo<>(all);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(shopPoPageInfo);
    }

    //2. 根据状态进行查询显示数据
    @PostMapping("/shop/findByState")
    public CommonResult findByState(@RequestBody ShopBo shopBo){
        Integer state = shopBo.getState();
        if(state==0){

            List<ShopPo> bystate = shopService.findBystate(state);
            CommonResult commonResult = CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(bystate);
            return commonResult;
        }
        if(state==1){
            //System.out.println("停业");
            List<ShopPo> bystate = shopService.findBystate(state);
            CommonResult commonResult = CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(bystate);
            return commonResult;
        }
        if(state!=0 || state!=1){
            List<ShopPo> bystate = shopService.findAll();
            CommonResult commonResult = CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(bystate);
            return commonResult;
        }
        return null;
    }

    //3. 根据输入字段进行模糊查询
    @PostMapping("/shop/findByLike")
    public CommonResult findByLike(@RequestBody ShopBo shopBo){
        String inputTest = shopBo.getInputTest();
        if(inputTest!=null){
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(shopService.findByLike(inputTest));
        }
        return null;
    }

    //4. 新增一个门店
    @PostMapping("/shop/insertShopPo")
    public CommonResult insertShopPo(@RequestBody ShopBo shopBo){
        ShopPo shopPo = shopBo.getShopPo();
        System.out.println(shopPo);
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(shopService.insertShopPo(shopPo));
    }

    //5. 修改门店信息
    @PostMapping("/shop/updateShopPo")
    public CommonResult updateShopPo(@RequestBody ShopBo shopBo){
        Integer row = shopService.updateShopPo(new ShopPo());
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(row);

    }

    //6. 删除门店信息
    @PostMapping("/shop/deleteShopPo")
    public CommonResult deleteShopPo(@RequestBody ShopBo shopBo){
        Integer pageIndex = shopBo.getPageIndex();
        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setData(pageIndex);
    }
}
